package com.mucd.chat.service;

import cn.hutool.core.util.ObjectUtil;
import com.mucd.chat.mapper.MessageListMapper;
import com.ruoyi.common.core.domain.MessageList;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.MessageListModel;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * @author mucd
 */
@Service
@Slf4j
public class MessageListService {

    @Resource
    private MessageListMapper messageListMapper;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private MessageRecordService messageRecordService;


    /**
     * 首页消息列表
     *
     * @param myUserId     发送人id
     * @param targetUserId 接收人id
     * @param message      消息内容
     */
    @Async
    public void saveMessageList(Long myUserId, Long targetUserId, String message) {
        MessageList messageList = new MessageList();
        messageList.setSendUserId(myUserId);
        messageList.setReceiveUserId(targetUserId);
        messageList.setMessage(message);

        MessageList dbRes = messageListMapper.selectListBySendUserIdAndReceiveUserId(messageList);
        if (ObjectUtil.isNull(dbRes)) {
            messageListMapper.insert(messageList);
            return;
        }

        messageListMapper.updateBySendUserIdAndReceiveUserId(messageList);

    }


    /**
     * 根据自己的id查询聊天列表
     * todo 会把自己和对方的消息都查出来
     *
     * @return 聊天列表
     */
    public List<MessageListModel> selectMessageList() {

        long myUserId = SecurityUtils.getUserId();

        List<MessageListModel> msgList = messageListMapper.selectMessageList(myUserId);


        Iterator<MessageListModel> iterator = msgList.iterator();
        while (iterator.hasNext()) {
            MessageListModel item = iterator.next();
            //todo 临时解决方案，可能会有BUG
            if (item.getSendUserId().equals(myUserId)) {
                iterator.remove();
                continue;
            }
            int num = messageRecordService.selectNotReadNum(myUserId);

            SysUser sysUser = sysUserService.selectUserById(item.getSendUserId());
            item.setAvatar(sysUser.getAvatar());
            item.setNickName(sysUser.getNickName());
            item.setUserId(sysUser.getUserId());
            item.setNotReadNum(num);
        }

        return msgList;
    }

    /**
     * 清除首页列表未读数量
     *
     * @param model 好友id
     * @return 消除的数量
     */
    public Object clearMessageNotRead(MessageListModel model) {
        long myUserId = SecurityUtils.getUserId();
        //登陆者是接收人, 消息列表展示的是发送人
        model.setReceiveUserId(myUserId);
        model.setSendUserId(model.getFriendId());

        int clearNum = messageListMapper.clearMessageNotRead(model);
        log.info("清除发送人{},接收人{}的未读数量成功，数量为：{}", model.getSendUserId(), model.getReceiveUserId(), clearNum);

        return clearNum;
    }
}
